package com.comdo.newup_loan.widget.mySpinner;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.widget.WheelView;

import java.util.ArrayList;
import java.util.List;


/*
 * 通用的滑动选择器
 */
public class MySpinner<T> {
	private List<ComomData> mObjects = new ArrayList<ComomData>();
	private String[] Strs = {};
	private Dialog dialog_mySpinner;
	private WheelView wheelViewOne;

	public WheelView getWheelViewOne() {
		return wheelViewOne;
	}

	public void setWheelViewOne(WheelView wheelViewOne) {
		this.wheelViewOne = wheelViewOne;
	}

	public WheelView getWheelViewTwo() {
		return wheelViewTwo;
	}

	public void setWheelViewTwo(WheelView wheelViewTwo) {
		this.wheelViewTwo = wheelViewTwo;
	}

	public WheelView getWheelViewThree() {
		return wheelViewThree;
	}

	public void setWheelViewThree(WheelView wheelViewThree) {
		this.wheelViewThree = wheelViewThree;
	}

	private WheelView wheelViewTwo;
	private WheelView wheelViewThree;
	private Context context;
	private int total = 1;
	private int flagone = -1;
	private LinearLayout ll_content;
	private onResultListener listener;
	private String suffix="";

	@SuppressWarnings("unchecked")
	public MySpinner(Context context, List<T> mObjects, int total) {
		this.context = context;
		this.total = total;
		this.mObjects = (List<ComomData>) mObjects;
		selectAddressDialog(context);
	}
	/*
	 * 数组的构造方法
	 * @param suffix item显示后缀
	 */
	public MySpinner(Context context, String[] strs, int total,String suffix) {
		this.context = context;
		this.total = total;
		this.Strs = strs;
		this.suffix=suffix;
		ArrayToList();
		selectAddressDialog(context);
	}

	private void ArrayToList() {
		ArrayToListBean atb;
		mObjects = new ArrayList<ComomData>();
		if (Strs.length != 0) {
			for (int i = 0; i < Strs.length; i++) {
				atb = new ArrayToListBean();
				atb.setId(i);
				atb.setName(Strs[i]+suffix);
				mObjects.add((ComomData) atb);
			}
		}

	}
	//显示选择窗体
	public void show() {
		if (dialog_mySpinner != null)
			dialog_mySpinner.show();
	}
	//根据id复位-----暂时支持一级
	public void selectItemById(int id) {
		int i=0;
		for(ComomData data:mObjects ){
			
			if(data.getMyId()==id){
				wheelViewOne.setSeletion(i);
			}
			i++;
		}
		
	}
	//根据name复位----暂时支持一级
	public void selectItemByName(String name) {
		int i=0;
		for(ComomData data:mObjects ){
			if(data.getMyName().equals(name)){
				wheelViewOne.setSeletion(i);
			}
			i++;
		}
	}
	public void selectAddressDialog(Context context) {
		dialog_mySpinner = new Dialog(context, R.style.MyDialogStyle);
		dialog_mySpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.picker_myspinner, null);
		ll_content = (LinearLayout) view.findViewById(R.id.ll_content);
		TextView btn_confirm = (TextView) view.findViewById(R.id.btn_confirm);
		TextView btn_cancle = (TextView) view.findViewById(R.id.btn_cancle);
		btn_confirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				listener.onResult(geResult());
				if (dialog_mySpinner != null)
					dialog_mySpinner.dismiss();
			}
		});
		btn_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (dialog_mySpinner != null)
					dialog_mySpinner.dismiss();
			}
		});
		Window window = dialog_mySpinner.getWindow();
		// 重新设置
		WindowManager.LayoutParams lp = window.getAttributes();
		window.setGravity(Gravity.BOTTOM);
		window.setWindowAnimations(R.style.addresspickerstyle); // 添加动画
		window.setAttributes(lp);
		dialog_mySpinner.setContentView(view);
		/*
		 * 将对话框的大小按屏幕大小的百分比设置
		 */
		WindowManager m = window.getWindowManager();
		Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
		WindowManager.LayoutParams p = window.getAttributes(); // 获取对话框当前的参数值
		// p.height = (int) (d.getHeight() * 0.4); // 高度设置为屏幕的0.6
		p.width = (int) (d.getWidth() * 1.0); // 宽度设置为屏幕的0.65
		window.setAttributes(p);
		lp.x = 0; // 新位置X坐标
		lp.y = 0; // 新位置Y坐标
		init();
		dialog_mySpinner.show();
	}

	private Object geResult() {
		if (mObjects == null || mObjects.size() == 0)
			return null;
		int i = 0;
		if (!wheelViewOne.getSeletedItem().equals(""))
			i = 1;
		if (total > 1) {
			if (!wheelViewTwo.getSeletedItem().equals(""))
				i = 2;
		}
		if (total > 2) {
			if (!wheelViewThree.getSeletedItem().equals(""))
				i = 3;
		}
		if (i == 0) {
			return null;
		} else if (i == 1) {
			return wheelViewOne.getSeletedItem();
		} else if (i == 2) {
			return wheelViewTwo.getSeletedItem();
		} else if (i == 3) {
			return wheelViewThree.getSeletedItem();
		} else {
			return null;
		}
	}

	public void setOnResultListener(onResultListener listener) {
		this.listener = listener;
	}

	private void init() {
		int screenWidth = ((Activity) context).getWindowManager()
				.getDefaultDisplay().getWidth();
		LayoutParams lp = new LinearLayout.LayoutParams(screenWidth / total,
				LayoutParams.MATCH_PARENT);
		wheelViewOne = new WheelView(context);
		wheelViewTwo = new WheelView(context);
		wheelViewThree = new WheelView(context);
		wheelViewOne.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, Object item) {
				flagone = selectedIndex - 1;
				switch (total) {

				case 2:
					if (null != mObjects.get(selectedIndex - 1).getdata()
							&& mObjects.get(selectedIndex - 1).getdata().size() > 0) {
						wheelViewTwo.setItems(mObjects.get(selectedIndex - 1)
								.getdata());
					}
					break;
				case 3:
					if (null != mObjects.get(selectedIndex - 1).getdata()
							&& mObjects.get(selectedIndex - 1).getdata().size() > 0) {
						wheelViewTwo.setItems(mObjects.get(selectedIndex - 1)
								.getdata());
						wheelViewTwo.invalidate();
						List<ComomData> obj = mObjects.get(0).getdata();

						if (obj.get(0).getdata() != null
								&& obj.get(0).getdata().size() > 0) {

							wheelViewThree.setItems(obj.get(0).getdata());
						} else {

							wheelViewThree.setItems(new ArrayList<ComomData>());
						}
					}

					break;

				default:
					break;
				}
			}
		});
		wheelViewTwo.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, Object item) {
				switch (total) {
				case 3:
					List<ComomData> obj = mObjects.get(flagone).getdata();

					if (obj.get(selectedIndex - 1).getdata() != null
							&& obj.get(selectedIndex - 1).getdata().size() > 0) {

						wheelViewThree.setItems(obj.get(selectedIndex - 1)
								.getdata());

					} else {
						wheelViewThree.setItems(new ArrayList<ComomData>());
					}
					break;

				default:
					break;
				}
			}
		});
		wheelViewThree.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
			@Override
			public void onSelected(int selectedIndex, Object item) {
			}
		});

		switch (total) {
		case 1:
			ll_content.addView(wheelViewOne, lp);
			wheelViewOne.setItems(mObjects);
			break;
		case 2:
			ll_content.addView(wheelViewOne, lp);
			if (mObjects != null && mObjects.size() > 0) {
				wheelViewOne.setItems(mObjects);
				ll_content.addView(wheelViewTwo, lp);
				if (mObjects.get(0).getdata() != null
						&& mObjects.get(0).getdata().size() > 0)
					wheelViewTwo.setItems(mObjects.get(0).getdata());
			}
			break;
		case 3:
			ll_content.addView(wheelViewOne, lp);
			if (mObjects != null && mObjects.size() > 0) {
				wheelViewOne.setItems(mObjects);
				ll_content.addView(wheelViewTwo, lp);
				if (mObjects.get(0).getdata() != null
						&& mObjects.get(0).getdata().size() > 0) {
					wheelViewTwo.setItems(mObjects.get(0).getdata());
					ll_content.addView(wheelViewThree, lp);
					List<ComomData> obj = mObjects.get(0).getdata();
					if (obj.get(0).getdata() != null
							&& obj.get(0).getdata().size() > 0) {
						wheelViewThree.setItems(obj.get(0).getdata());
					} else {
						wheelViewThree.setItems(new ArrayList<ComomData>());
					}
				}
			}
			break;
		default:
			break;
		}

	}
}
